import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../models/index.dart';

/// 活动卡片组件
/// 
/// 功能：
/// - 展示活动封面图
/// - 显示活动类型标签
/// - 显示活动标题、描述
/// - 显示发布者信息和参与人数
/// - 支持点击回调
/// 
/// 使用 flutter_screenutil 进行屏幕适配：
/// - .w: 宽度适配
/// - .h: 高度适配
/// - .r: 半径/间距适配（推荐用于 padding、margin、borderRadius）
/// - .sp: 字体大小适配
class ActivityCard extends StatelessWidget {
  final ActivityEntity activity;
  final VoidCallback? onTap;

  const ActivityCard({
    super.key,
    required this.activity,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    // 获取封面图
    final coverImage = activity.activityImages?.isNotEmpty == true
        ? activity.activityImages!.first
        : '';
    
    // 获取活动类型
    final activityType = activity.activityType?.isNotEmpty == true
        ? activity.activityType!.first
        : '双方商议';
    
    // 活动类型颜色
    Color tagColor = Colors.red;
    if (activityType == 'AA') {
      tagColor = Colors.orange;
    } else if (activityType == '我买单') {
      tagColor = Colors.green;
    }
    
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 8.r,
              offset: Offset(0, 2.h),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 封面图 - 使用 Expanded 自适应高度
            Expanded(
              flex: 3,
              child: _buildCoverImage(coverImage, activityType, tagColor),
            ),
            
            // 活动信息
            _buildActivityInfo(),
          ],
        ),
      ),
    );
  }

  /// 构建封面图
  Widget _buildCoverImage(String coverImage, String activityType, Color tagColor) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        children: [
          // 图片或占位容器
          ClipRRect(
            borderRadius: BorderRadius.vertical(top: Radius.circular(12.r)),
            child: Container(
              width: double.infinity,
              height: double.infinity,
              child: coverImage.isNotEmpty
                  ? CachedNetworkImage(
                      imageUrl: coverImage,
                      width: double.infinity,
                      height: double.infinity,
                      fit: BoxFit.cover,
                      placeholder: (context, url) => Container(
                        width: double.infinity,
                        height: double.infinity,
                        color: Colors.grey[200],
                        child: Center(
                          child: SizedBox(
                            width: 20.w,
                            height: 20.h,
                            child: const CircularProgressIndicator(strokeWidth: 2),
                          ),
                        ),
                      ),
                      errorWidget: (context, url, error) => Container(
                        width: double.infinity,
                        height: double.infinity,
                        color: Colors.grey[200],
                        child: Center(
                          child: Icon(Icons.broken_image, size: 32.r, color: Colors.grey[400]),
                        ),
                      ),
                    )
                  : Container(
                      width: double.infinity,
                      height: double.infinity,
                      color: Colors.grey[200],
                      child: Center(
                        child: Icon(Icons.image, size: 48.r, color: Colors.grey[400]),
                      ),
                    ),
            ),
          ),
          
          // 活动类型标签 - 右上角（始终显示）
          Positioned(
            top: 6.h,
            right: 6.w,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 4.h),
              decoration: BoxDecoration(
                color: tagColor,
                borderRadius: BorderRadius.circular(8.r),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.2),
                    blurRadius: 4.r,
                    offset: Offset(0, 2.h),
                  ),
                ],
              ),
              child: Text(
                activityType,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 10.sp,
                  fontWeight: FontWeight.bold,
                  height: 1.0,
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 构建活动信息
  Widget _buildActivityInfo() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 8.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          // 活动标题
          Text(
            activity.activityTitle,
            style: TextStyle(
              fontSize: 13.sp,
              fontWeight: FontWeight.w500,
              color: Colors.black87,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          
          SizedBox(height: 3.h),
          
          // 活动描述
          Text(
            activity.activityDetails ?? '',
            style: TextStyle(
              fontSize: 11.sp,
              color: Colors.grey[600],
              height: 1.2,
            ),
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
          
          SizedBox(height: 6.h),
          
          // 底部信息
          _buildBottomInfo(),
        ],
      ),
    );
  }

  /// 构建底部信息
  Widget _buildBottomInfo() {
    return Row(
      children: [
        // 发布者头像
        if (activity.publisherAvatar != null && activity.publisherAvatar!.isNotEmpty)
          CircleAvatar(
            radius: 9.r,
            backgroundImage: CachedNetworkImageProvider(activity.publisherAvatar!),
          )
        else
          CircleAvatar(
            radius: 9.r,
            backgroundColor: Colors.grey[300],
            child: Text(
              (activity.publisherNickname ?? '?')[0],
              style: TextStyle(fontSize: 9.sp),
            ),
          ),
        SizedBox(width: 4.w),
        // 发布者昵称
        Expanded(
          child: Text(
            activity.publisherNickname ?? '未知用户',
            style: TextStyle(
              fontSize: 10.sp,
              color: Colors.grey[600],
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        SizedBox(width: 4.w),
        // 参与人数
        Text(
          '${activity.currentParticipants ?? 0}/${activity.activityMaxParticipants ?? 10}人',
          style: TextStyle(
            fontSize: 10.sp,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }
}

